import axios from './index'

export const uploadFile = formData => {
  const res = axios.request({
    method: 'post',
    url: '/upload',
    data: formData,
    headers: {'Content-Type': 'multipart/form-data'}
  })
  return res
}

export const postJson = (url, data, responseType = 'json') => {
  return axios({
    method: 'post',
    url,
    data: data,
    headers: {'Content-Type': 'application/json'},
    responseType
  })
}

export const deleteJson = (url, data, responseType = 'json') => {
  return axios({
    method: 'delete',
    url,
    params: data,
    headers: {'Content-Type': 'application/json'},
    responseType
  })
}

export const getJson = (url, data, responseType = 'json') => {
  return axios({
    method: 'get',
    url,
    params: data,
    headers: {'Content-Type': 'application/json'},
    responseType
  })
}

const postFile = (url, obj, files) => {
  let data = new FormData()
  // data.append('token', store.state.token)
  for (var key in obj) {
    if (typeof obj[key] === 'object') {
      data.append(key, JSON.stringify(obj[key]))
    } else {
      data.append(key, obj[key])
    }
  }
  files.forEach((val, i) => {
    data.append('files', val.raw)
  })
  // 附件以base64形式上传（如：流程里面的高拍仪）
  return axios({
    method: 'post',
    data: data,
    withCredentials: false,
    headers: {'Content-Type': 'multipart/form-data'}
  })
}

export const api = {
  // 注册
  register: (data) => postJson('/register', data),
  // 登录
  login: (data) => postJson('/login', data),
  // 新增文章
  addArticle: (data) => postJson('/article', data),
  // 有道
  createBlogFromYoudao: () => postJson('/article/youdao'),
  // 获取文章列表
  getArticleList: (data) => getJson('/article/list', data),
  // 获取文章详情
  getArticleById: (data) => getJson(`/article/${data}`),
  // 删除文章
  deleteArticleById: (data) => deleteJson(`/article/${data}`),
  // 修改文章
  updateArticleById: (data) => postJson(`/article/${data.id}`, data.content),
  // 导出单个文章
  exportArticleById: (data) => getJson(`/article/output/${data.id}`, data.content, 'blob'),
  // 导出所有文章
  exportArticleAll: (data) => getJson(`/article/output/all`, data, 'blob'),
  // 获取用户列表
  getUserList: (data) => getJson('/user/list', data),
  // 更新用户
  updateUserById: (data) => postJson(`/user/${data.id}`, data.content),
  // 删除用户
  deleteUserById: (data) => deleteJson(`/user/${data}`),
  postDiscuss: (data) => postJson(`/discuss/`, data),
  deleteComment: (data) => postJson(`/discuss/comment/${data}`),
  deleteReply: (data) => postJson(`/discuss/reply/${data}`),
  getCategoryList: (data) => getJson('/category/list', data),
  getTagList: (data) => getJson('/tag/list', data),
  // 获取文章结构列表
  getTreeList: (data) => getJson('/tree/list', data),
  // 新增tree节点
  addTreeNode: (data) => postJson('/tree/addnode', data),
  // 更新tree节点
  updateTreeNode: (data) => postJson('/tree/updateNode', data),
  // 更新tree节点
  deleteTreeById: (data) => deleteJson(`/tree/${data}`),

  postPriceRule: (obj, data) => postFile('/price/upload', obj, data),
  // 获取质量比较好的客户，购买次数大于2次
  getKindBuyer: (data) => postJson('/kindBuyer', data),
  // 获取用户烟弹的购买记录和消耗速度
  getBuyerConsume: (data) => postJson('/buyerConsume', data),
  // 添加用户权限
  postPermission: (data) => postJson('/addPermission', data),
  // 根据用户ID获取权限控制
  getPermissionById: (data) => postJson('/permissionById', data),
  // 根据用户ID更新权限控制
  updatePermissionById: (data) => postJson('/updatePermissionById', data),
  // 获取商品列表
  getGoodsList: (data) => postJson('/price/list', data),
  // 获取商品列表(新规则)
  getNewGoodsList: (data) => postJson('/goods/list', data),
  // 根据用户ID更新物品清单
  updateGoodsById: (data) => postJson('/goods/updateGoodsById', data),
  // 复刻订单
  copyOrders: (data) => postJson('/copyOrders', data, 'blob'),
  // 新增商品列表(新规则)
  addGoodsList: (data) => postJson('/goods/addGoods', data),
  // 修改权限控制
  editPermissionById: (data) => postJson('/editPermissionById', data),
  // 检查邮箱是否可用于注册
  checkEmailValid: (data) => postJson('user/checkEmail', data),
  // 检查手机是否可用于注册
  checkPhoneValid: (data) => postJson('user/checkPhone', data),
  // 发送手机短信, 短信类型type: * 1-注册 * 2-身份认证 * 3-登录确认 * 4-修改密码 * 5-修改信息
  sendPhoneMsg: (data) => postJson('sys/sendCode', data),
  // 发送邮箱短信
  sendEmailCode: (data) => postJson('sys/sendEmailCode', data),
  // 手机注册
  registerByPhone: (data) => postJson('user/register', data),
  // 邮箱注册
  registerByEmail: (data) => postJson('user/registerByEmail', data),
  // 通过appId获取app详情
  findAppByid: (id) => postJson(`publish/findAppByid/${id}`),
  // 通过appId删除app
  deleteAppByid: (id) => postJson(`publish/delete/${id}`),
  // 校验code
  checkCode: (code) => postJson(`publish/checkCode/${code}`),
  // 获取可以合并的app列表
  toBeMergedList: (id) => postJson(`publish/toBeMergedList/${id}`),
  // 取消合并
  cancelMerge: (id) => postJson(`publish/cancelMerge/${id}`),
  // 合并APP
  merge: (mergeAndroidId, mergeIOSId) => postJson(`publish/merge/${mergeAndroidId}/${mergeIOSId}`)
}
